package cn.hallele.leinsteining.ms.auditcenter.domain.log.das;

import cn.hallele.leinsteining.mc.infrastructure.metadata.io.Page;
import cn.hallele.leinsteining.ms.auditcenter.cache.LogCache;
import cn.hallele.leinsteining.ms.auditcenter.domain.log.domain.LogDomain;
import cn.hallele.leinsteining.ms.auditcenter.entity.LogEntity;
import cn.hallele.leinsteining.ms.support.starter.components.convertor.ModelDas;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 日志数据操作服务
 *
 * @author anle5
 * @since 2024/8/4 21:52
 */
@Slf4j
@Component
public class LogDomainDas extends ModelDas<LogCache, LogDomain, LogEntity> {
    public LogDomainDas(LogCache logCache) {
        super(logCache);
    }

    /**
     * 查询单个日志记录
     *
     * @param condition 查询条件
     * @return 单个日志记录
     */
    public LogDomain findOne(LogDomain condition) {
        return revert(this.option.findOne(convert(condition)));
    }

    /**
     * 查询单个日志记录
     *
     * @param condition 查询条件
     * @return 单个日志记录
     */
    public Page<LogDomain> findAllByPage(LogDomain condition, int num, int page) {
        return revertPage(this.option.findAllByCondition(convert(condition), num, page));
    }

    /**
     * 添加单个日志记录
     *
     * @param logDomain logDomain
     * @return 单个日志记录
     */
    public LogDomain saveOne(LogDomain logDomain) {
        return revert(this.option.addOne(convert(logDomain)));
    }

    /**
     * 将T转成E
     *
     * @param logDomain 被转换者
     * @return E 转换后者
     */
    @Override
    protected LogEntity convert(LogDomain logDomain) {
        return logDomain == null ? null : LogEntity.builder()
                .id(logDomain.getId())
                .appName(logDomain.getAppName())
                .apiId(logDomain.getApiId())
                .tranceId(logDomain.getTranceId())
                .ip(logDomain.getIp())
                .device(logDomain.getDevice())
                .initiator(logDomain.getInitiator())
                .request(logDomain.getRequest())
                .response(logDomain.getResponse())
                .requestStartTime(logDomain.getRequestStartTime())
                .requestEndTime(logDomain.getRequestEndTime())
                .createTime(logDomain.getCreateTime())
                .createBy(logDomain.getCreateBy())
                .build();
    }

    /**
     * 将E回转陈T
     *
     * @param logEntity 被回转换者
     * @return E 回转换后者
     */
    @Override
    protected LogDomain revert(LogEntity logEntity) {
        return logEntity == null ? null : LogDomain.builder()
                .apiId(logEntity.getApiId())
                .appName(logEntity.getAppName())
                .id(logEntity.getId())
                .tranceId(logEntity.getTranceId())
                .ip(logEntity.getIp())
                .device(logEntity.getDevice())
                .initiator(logEntity.getInitiator())
                .request(logEntity.getRequest())
                .response(logEntity.getResponse())
                .requestStartTime(logEntity.getRequestStartTime())
                .requestEndTime(logEntity.getRequestEndTime())
                .createTime(logEntity.getCreateTime())
                .createBy(logEntity.getCreateBy())
                .build();
    }
}
